﻿using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Dapper_Core.Model;

namespace Dapper_Core.Common
{
    public class NlogUtility
    {
        /// <summary>
        /// 写异常日志
        /// </summary>
        /// <param name="loggerName">在哪个类中出现的异常，System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName</param>
        /// <param name="content">异常的描述内容</param>
        /// <param name="data">相关的参数</param>
        /// <param name="ex">异常的详细信息</param>
        /// <param name="application">Application的参数值</param>
        public static void WriteError(string loggerName, string content, string data, Exception ex, string application)
        {
            try
            {
                LogEventInfo ei = LogEventInfo.Create(NLog.LogLevel.Error, loggerName, ex, null, content);
                ei.Properties["ip"] = IPUtility.GetVisitorRealIP();
                ei.Properties["application"] = application;
                ei.Properties["data"] = data;
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.Log(ei);
            }
            catch (Exception nlogEx)
            {
                WriteFileLog("WriteError()NLog插入日志时出现异常，nlogEx=" + nlogEx.ToString());
            }
        }

        /// <summary>
        /// 写普通信息日志
        /// </summary>
        /// <param name="loggerName">在哪个类中出现的异常，System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName</param>
        /// <param name="content">异常的描述内容</param>
        /// <param name="data">相关的参数</param>
        /// <param name="application">Application的参数值</param>
        public static void WriteInfo(string loggerName, string content, string data, string application)
        {
            try
            {
                LogEventInfo ei = LogEventInfo.Create(NLog.LogLevel.Info, loggerName, null, content);
                ei.Properties["ip"] = IPUtility.GetVisitorRealIP();
                ei.Properties["application"] = application;
                ei.Properties["data"] = data;
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.Log(ei);
            }
            catch (Exception ex)
            {
                WriteFileLog("WriteInfo()NLog插入日志时出现异常，ex=" + ex.ToString());
            }
        }

        /// <summary>
        /// 写文件日志
        /// </summary>
        /// <param name="content">内容</param>
        public static void WriteFileLog(string content, string folder = "")
        {
            string fileLogPath = @"C:\VSLog";
            if (!folder.IsEmpty())
            {
                fileLogPath = Path.Combine(fileLogPath, folder);
            }
            if (!Directory.Exists(fileLogPath))
            {
                Directory.CreateDirectory(fileLogPath);
            }
            StreamWriter sw = new StreamWriter(Path.Combine(fileLogPath, DateTime.Now.ToString(@"yyyyMMdd_") + "DapperCore.txt"), true);
            sw.WriteLine($"【{DateTime.Now.ToString()}--{IPUtility.GetVisitorRealIP()}】 " + content);
            sw.Close();
        }
    }
}
